import requests
from wechatpy.oauth import WeChatOAuth

from flask import g, current_app, request, make_response, jsonify
from flask_restplus import Resource, marshal
from sqlalchemy import or_

# 业务代码
from hzb.database import db
from hzb.label import label_ns
from hzb.auth import auth
from hzb.common.field import BaseResp

from hzb.label.logic.label_logic import LabelLogic
from hzb.label.model.label_model import LabelModel

# 模块内部

from ..filed.label_filed import LabelGetLabelsReq, LabelGetLabelsResp, SetLabelsReq, SetLabelsResp, AddLabelsReq, \
    DelLabelsReq, Get24TimeLabelsResp, GetRecordByTimeReq, GetRecordByTimeResp


# 资源没有继承的必要，相关操作必须转移到各自的类中
@label_ns.route('/get_labels')
class GetLabels(Resource):
    # 格式化返回数据
    @auth.login_required
    @label_ns.expect(LabelGetLabelsReq, validate=True)
    @label_ns.marshal_with(LabelGetLabelsResp)
    def post(self):
        """
        get_labels
        """
        _user_id = g.user.id
        _resp_obj = LabelLogic().get_labels(_user_id)
        _result = dict()
        _result['data'] = _resp_obj
        return _result


@label_ns.route('/set_run_label')
class SetRunLabel(Resource):
    # 格式化返回数据
    @auth.login_required
    @label_ns.expect(SetLabelsReq, validate=True)
    @label_ns.marshal_with(SetLabelsResp)
    def post(self):
        """
        set_run_label
        """
        _user_id = g.user.id
        _label_name = request.json.get('label_name')
        _choose_time = LabelLogic().set_run_label(_user_id, _label_name)
        if _choose_time > 0:
            _result = dict()
            _result['choose_time'] = _choose_time
            return _result
        else:
            _result = dict()
            _result['status'] = 1000  # 状态码
            _result['message'] = '设置标签出现问题'
            return _result


@label_ns.route('/add_label')
class AddLabel(Resource):
    # 格式化返回数据
    @auth.login_required
    @label_ns.expect(AddLabelsReq, validate=True)
    @label_ns.marshal_with(BaseResp)
    def post(self):
        """
        add_label
        """
        _user_id = g.user.id
        _label_name = request.json.get('label_name')

        _label_model = LabelModel.query.filter_by(user_id=_user_id,label_name=_label_name).first()
        if _label_model is not None:
            _result = dict()
            _result['status'] = 1001  # 状态码
            _result['message'] = '标签名称已存在'
            return _result
        if LabelLogic().add_label(_user_id, _label_name) is True:
            _result = dict()
            return _result
        else:
            _result = dict()
            _result['status'] = 1002  # 状态码
            _result['message'] = '数据错误'
            return _result


@label_ns.route('/del_labels')
class DelLabels(Resource):
    # 格式化返回数据
    @auth.login_required
    @label_ns.expect(DelLabelsReq, validate=True)
    @label_ns.marshal_with(BaseResp)
    def post(self):
        """
        add_label
        """
        _user_id = g.user.id
        _label_names = request.json.get('label_names')
        print("DelLabels", _label_names)
        if LabelLogic().del_labels(_user_id, _label_names) is True:
            _result = dict()
            return _result
        else:
            _result = dict()
            _result['status'] = 1002  # 状态码
            _result['message'] = '数据错误'
            return _result


@label_ns.route('/get_24_time_record')
class Get24TimeRecord(Resource):
    # 格式化返回数据
    @auth.login_required
    @label_ns.marshal_with(Get24TimeLabelsResp)
    def post(self):
        """
        add_label
        """
        _user_id = g.user.id
        _labels = LabelLogic().get_24_time_record_labels(_user_id)
        _result = dict()
        _result['labels'] = _labels
        return _result


@label_ns.route('/get_record_by_time')
class GetRecordByTime(Resource):
    # 格式化返回数据
    @auth.login_required
    @label_ns.expect(GetRecordByTimeReq, validate=True)
    @label_ns.marshal_with(GetRecordByTimeResp)
    def post(self):
        """
        add_label
        """
        _user_id = g.user.id
        _begin_time = request.json.get('begin_time')
        _end_time = request.json.get('end_time')
        _labels = LabelLogic().get_record_by_time(_user_id, _begin_time, _end_time)
        _result = dict()
        _result['labels'] = _labels
        return _result

